package com.xywy.find.paser;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.xywy.R;
import com.xywy.common.net.AsyncHttpClient;
import com.xywy.common.net.OnResultListenerAdapter;
import com.xywy.common.net.Parser;
import com.xywy.common.util.MLog;
import com.xywy.find.bean.Jkgc;
import com.xywy.find.bean.JkgcData;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

/**
 * Created by xiaogd on 8/4/14. 健康广场 - json解析类
 */
public class JkgcPaser {

	private static final String TAG = "JkgcPaser";
	public static final int FAILURE = 0;
	public static final int SUCCESS = 1;
	private Context mContext;
	private Handler mHandler;

	public JkgcPaser(Context context, Handler handler) {
		this.mContext = context;
		this.mHandler = handler;
	}

	// 封装Parser参数
	private Parser paramsParser;
	// 封装httpclient参数
	private HashMap<String, Object> params;
	// 异步加载
	private AsyncHttpClient asyncHttpClient;

	// 处理网络请求
	public void sendMsg(String plazeId, String curPage) {
		paramsParser = new Parser();
		// 封装参数
		params = new HashMap<String, Object>();
		// 请求路径
		paramsParser.action = mContext.getString(R.string.find_jkgc);
		// 请求方式
		paramsParser.request = "get";

		params.put("dir", plazeId);
		params.put("page", curPage);

		paramsParser.map = params;
		// 传递上下文
		paramsParser.context = this.mContext;

		asyncHttpClient = AsyncHttpClient.getInstance();
		asyncHttpClient.exeute(paramsParser, onResultListenerAdapter);
	}

	OnResultListenerAdapter onResultListenerAdapter = new OnResultListenerAdapter() {
		@Override
		public <T extends Parser> void onSuccess(T resultParser, int resultCode) {
			super.onSuccess(resultParser, resultCode);
			Message message = new Message();
			message.what = SUCCESS;
			message.obj = resultParser.back;
			mHandler.sendMessage(message);

		}

		@Override
		public <T extends Parser> void onFailure(T parserResult, int resultCode) {
			Message message = new Message();
			message.what = FAILURE;
			message.obj = resultCode;
			mHandler.sendMessage(message);
		};
		public <T extends Parser> void onNoNetWork(T paramParser, int resultCode) {
			Message message = new Message();
			message.what = FAILURE;
			message.obj = resultCode;
			mHandler.sendMessage(message);
		};

	};
	private String[] titles;

	/**
	 * 封装返回的json
	 */
	private List<Jkgc> getJkgc(String jsonStr) {
		List<Jkgc> list = Collections.synchronizedList(new ArrayList<Jkgc>());
		// 分别实例化，进行潜拷贝
		ArrayList<JkgcData> dataList = new ArrayList<JkgcData>();
		Jkgc jkgc = new Jkgc();
		JkgcData jkgcData = new JkgcData();

		try {
			JSONObject jsonObject = new JSONObject(jsonStr);
			String state = jsonObject.getString("state");
			if (state.equals("200")) {

				JSONArray jsonArray = jsonObject.getJSONArray("type");
				if (null != jsonArray && jsonArray.length() > 0) {
					titles = new String[jsonArray.length()];

					for (int i = 0; i < jsonArray.length(); i++) {
						JSONObject json = jsonArray.getJSONObject(i);
						Jkgc gc = jkgc.clone();
						gc.setTypename(json.optString("typename"));

						titles[i] = json.optString("typename");
						JSONArray dataArray = json.optJSONArray("data");
						// MLog.i("JKGCpASER","JK======PASER====="+dataArray.toString());

						if (dataArray != null && dataArray.length() > 0) {
							ArrayList<JkgcData> cloneList = (ArrayList<JkgcData>) dataList
									.clone();
							for (int j = 0; j < dataArray.length(); j++) {
								JSONObject jsonData = dataArray
										.getJSONObject(j);
								JkgcData cloneData = jkgcData.clone();
								cloneData.setTitle(jsonData.optString("title"));
								cloneData.setContentid(jsonData
										.optString("contentid"));
								cloneData.setTime(jsonData.optString("time"));
								cloneData.setPreview(jsonData
										.optString("preview"));
								cloneData.setTypeid(jsonData
										.optString("typeid"));
								cloneList.add(cloneData);
							}
							gc.setData(cloneList);
						}

						list.add(gc);
					}
				}
			} else {
				Log.d(TAG, "当前接口请求异常，返回状态码为：" + state);
			}

		} catch (JSONException e) {
			e.printStackTrace();
		}
		return list;
	}
}
